﻿using System;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using FFIV.Lib.Controlers.Admin;
using FFIV.Lib.DataComponent;
using FFIV.Lib.IView.Articles;
using FFIV.Lib.Utilities;

public partial class Views_Articles_ArticlesManageList : System.Web.UI.UserControl, IArticlesManage
{
	private ManageArticleController m_Controller;

	public ManageArticleController Controller
	{
		set { m_Controller = value; }
	}

	public AritcleDS.ArticlesDetailDataTable ArticleManageList
	{
		set
		{
			gv_ManageList.DataSource = value;
			gv_ManageList.DataBind();
			gv_ManageList.RowCommand += gv_ManageList_RowCommand;
		}
	}

	protected void Page_Load(object sender, EventArgs e)
	{
	}

	public void RegisterEvents()
	{
		gv_ManageList.RowDataBound += gv_ManageList_RowDataBound;
		gv_ManageList.PageIndexChanging += gv_ManageList_PageIndexChanging;
	}

	private void gv_ManageList_PageIndexChanging(object sender, GridViewPageEventArgs e)
	{
		gv_ManageList.PageIndex = e.NewPageIndex;
		gv_ManageList.DataBind();
	}

	private void gv_ManageList_RowCommand(object sender, GridViewCommandEventArgs e)
	{
		if (e.CommandName == "del")
		{
			m_Controller.DeleteArticle(Convert.ToInt32(e.CommandArgument));
		}
	}

	private void gv_ManageList_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		HtmlAnchor temp_Title = e.Row.FindControl("lt_TItle") as HtmlAnchor;
		Literal temp_IsDraft = e.Row.FindControl("lt_IsDraft") as Literal;
		Literal temp_Author = e.Row.FindControl("lt_Author") as Literal;
		Literal temp_CreatDate = e.Row.FindControl("lt_CreatDate") as Literal;
		Literal temp_LastUpdateDate = e.Row.FindControl("lt_LastUpdateDate") as Literal;
		HtmlAnchor temp_Edit = e.Row.FindControl("lnk_Edit") as HtmlAnchor;
		LinkButton temp_Delete = e.Row.FindControl("lnk_Delete") as LinkButton;

		DataRowView rowView = (DataRowView)e.Row.DataItem;

		if (temp_Title != null)
		{
			temp_Title.InnerText = rowView["Title"].ToString();
			temp_Title.Title = rowView["Title"].ToString();
			temp_Title.HRef = UrlUtility.GetArticleDetailUrl(Convert.ToInt32(rowView["ArticleId"]));
		}

		if (temp_Author != null)
		{
			string author = rowView["AuthorName"].ToString();
			temp_Author.Text = author.Length > 9 ? author.Substring(0, 9) + "..." : author;
		}

		if (temp_IsDraft != null)
		{
			string draft = rowView["IsDraft"].ToString();
			if (draft == "False")
			{
				temp_IsDraft.Text = "否";
			}
			else
			{
				temp_IsDraft.Text = "是";
			}
		}

		if (temp_CreatDate != null)
		{
			temp_CreatDate.Text = ((DateTime)rowView["CreateDate"]).ToString("yyyy-MM-dd");
		}

		if (temp_LastUpdateDate != null)
		{
			temp_LastUpdateDate.Text = ((DateTime)rowView["LastUpdateDate"]).ToString("yyyy-MM-dd");
		}

		if (temp_Edit != null)
		{
			temp_Edit.HRef = UrlUtility.GetArticleEditUrl(Convert.ToInt32(rowView["ArticleId"]));
		}

		if (temp_Delete != null)
		{
			temp_Delete.CommandArgument = rowView["ArticleId"].ToString();
		}
	}
}
